package com.cskaoyan.controller.wx;

import com.cskaoyan.bean.BaseRespVo;
import com.cskaoyan.bean.pojo.MarketUser;
import com.cskaoyan.bean.vo.SearchIndexVo;
import com.cskaoyan.service.SearchService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**搜索模块
 * @author :LuoXuXin
 * @version : $version
 * @date :Created in 2022/4/8   22:08
 * @description:
 * @return:
 */
@RestController
@RequestMapping("wx/search")
public class SearchController {
    @Autowired
    SearchService searchService;


    @GetMapping("index")
    public BaseRespVo searchIndex() {
        Subject subject = SecurityUtils.getSubject();
        MarketUser principal = (MarketUser) subject.getPrincipal();
        if (principal != null && principal.getId() != -1) {
            SearchIndexVo searchIndexVo = searchService.searchIndex();
            return BaseRespVo.ok(searchIndexVo);
        } else {
            return BaseRespVo.fail(1000, "请登录哦亲");
        }

    }

    @GetMapping("helper")
    public BaseRespVo searchHelper(String keyword) {
        List<String> data = searchService.helperSearch(keyword);
        return BaseRespVo.ok(data);
    }

    //清空搜索历史
    @RequestMapping("clearhistory")
    public BaseRespVo clearHistory() {
        searchService.clearHistory();
        return BaseRespVo.ok();
    }

}
